package com.vancouver.knows.business.service.impl;

import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pangu.util.StringUtil;
import com.vancouver.knows.business.BUtils;
import com.vancouver.knows.business.bean.EventBannerInfo;
import com.vancouver.knows.business.bean.EventInfo;
import com.vancouver.knows.business.bean.EventItemInfo;

/**
 * 查询活动列表解析
 * <p/>
 * Created by cxlin
 */
public class FetchEventsHandler extends ACheckableJsonParser {

	public static final String KEY_POSTS = "posts";
	public static final String KEY_HEAD_POSTS = "head_posts";
	public static final String KEY_ALL_DATES = "all_dates";
	public static final String KEY_DATE = "date";

	private HashMap<String, Object> resultMap = new HashMap<String, Object>();

	@Override
	protected void parserContent(JSONObject content) throws Exception {
		String posts = content.getString("posts");

//		String allDates = content.getString("all_dates");
//		List<String> allDateList = JSON.parseArray(allDates, String.class);
//		if(allDateList == null){
//			allDateList = new ArrayList<>();
//		}
		List<String> allDateList = new ArrayList<>();
		resultMap.put(KEY_ALL_DATES, allDateList);

		HashMap<String,List<EventInfo>> eventInfoHashMap = new HashMap<>();
		resultMap.put(KEY_POSTS, eventInfoHashMap);
		HashMap<String,List<EventInfo>> dateHashMap = new HashMap<>();
		resultMap.put(KEY_DATE, dateHashMap);

		List<EventInfo> eventInfoList = JSON.parseArray(posts, EventInfo.class);
		if (eventInfoList != null) {
			Collections.sort(eventInfoList, new BUtils.EventInfoSorter());
			for (EventInfo eventInfo : eventInfoList) {
				String[] eventInTabs = eventInfo.getEventInTabs();
				for (String  tab : eventInTabs) {
					List<EventInfo> eventInfos = eventInfoHashMap.get(tab);
					if(eventInfos == null){
						eventInfos = new ArrayList<>();
						eventInfoHashMap.put(tab,eventInfos);
					}
					eventInfos.add(eventInfo);
				}

				String[] edg = eventInfo.getEdg();
				for (String str: edg) {
					str = BUtils.getEventPdg(str);
					str = str.substring(0,10);
					List<EventInfo> eventInfos = dateHashMap.get(str);
					if(eventInfos == null){
						eventInfos = new ArrayList<>();
						dateHashMap.put(str,eventInfos);
					}
					eventInfos.add(eventInfo);
				}
			}

			Iterator<String> keys = dateHashMap.keySet().iterator();
			while (keys.hasNext()){
				allDateList.add(keys.next());
			}
		}

		String headPosts = content.getString("head_posts");
		if (!StringUtil.isEmptyString(headPosts)) {
			List<EventBannerInfo> eventBannerInfoList = JSON.parseArray(headPosts, EventBannerInfo.class);
			resultMap.put(KEY_HEAD_POSTS, eventBannerInfoList);
		}
	}

	public HashMap<String, Object> getResultMap() {
		return resultMap;
	}

	@Override
	protected boolean isSuccess(int errorCode) {
		if (errorCode == 0 || errorCode == 6) {
			return true;
		}
		return false;
	}

}
